package zhr.chapter3.jiandanjisuanqi;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CalJFrame extends JFrame {
    private JTextField inputField1;
    private JTextField inputField2;
    private JComboBox<String> operatorComboBox;
    private JButton calculateButton;
    private JLabel resultLabel;
    public CalJFrame(){
        createUI();
    }

    public static void main(String[] args) {
        new CalJFrame();
    }

    private void createUI() {
        // 设置窗口标题
        setTitle("简单计算器");
        // 设置窗口关闭操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 设置窗口布局
        setLayout(new FlowLayout());

        // 创建第一个输入框
        inputField1 = new JTextField(5);
        add(inputField1);

        // 创建第二个输入框
        inputField2 = new JTextField(5);
        add(inputField2);

        // 创建运算符下拉选择框
        String[] operators = {"+", "-", "*", "/"};
        operatorComboBox = new JComboBox<>(operators);
        add(operatorComboBox);

        // 创建计算按钮
        calculateButton = new JButton("计算");
        add(calculateButton);

        // 创建结果显示标签
        resultLabel = new JLabel("结果: ");
        add(resultLabel);

        // 设置窗口大小
        setSize(300, 100);
        // 设置窗口可见
        setVisible(true);

        // 为计算按钮添加事件监听器
        calculateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {


            Expresion expresion=new Expresion();
            expresion.setNum1(Double.parseDouble(inputField1.getText()));
            expresion.setNum2(Double.parseDouble(inputField2.getText()));
            expresion.setSign(operatorComboBox.getSelectedItem().toString().charAt(0));
            resultLabel.setText(String.valueOf(expresion.getResult()));
            }
        });
    }
}
